package com.seepine.oss.service;

import com.alibaba.fastjson.JSONObject;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.BasicSessionCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.seepine.oss.OssProperties;
import com.seepine.oss.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.InputStream;

/**
 * @author seepine
 */
@Slf4j
public class QiNiuService {
	private OssProperties ossProperties;
	private UploadManager uploadManager;
	private Auth auth;

	/**
	 * QiNiuService
	 *
	 * @param ossProperties ossProperties
	 */
	public QiNiuService(OssProperties ossProperties) {
		this.ossProperties = ossProperties;
		Configuration cfg;
		switch (ossProperties.getRegion()) {
			case "huadong":
				cfg = new Configuration(Region.huadong());
				break;
			case "huabei":
				cfg = new Configuration(Region.huabei());
				break;
			case "huanan":
				cfg = new Configuration(Region.huanan());
				break;
			case "beimei":
				cfg = new Configuration(Region.beimei());
				break;
			case "xinjiapo":
				cfg = new Configuration(Region.xinjiapo());
				break;
			default:
				cfg = new Configuration(Region.autoRegion());
		}
		uploadManager = new UploadManager(cfg);
		auth = Auth.create(ossProperties.getSecretId(), ossProperties.getSecretKey());
	}

	/**
	 * @param bucket   bucket
	 * @param fileName fileName
	 * @param file     file
	 * @return filePath or prefix+filePath
	 */
	public String upload(String bucket, String fileName, File file) {
		try {
			String upToken = auth.uploadToken(bucket);
			Response response = uploadManager.put(file, fileName, upToken);
			DefaultPutRet putRet = JSONObject.parseObject(response.bodyString(), DefaultPutRet.class);
			return ossProperties.getPrefix() == null ? putRet.key : ossProperties.getPrefix() + putRet.key;
		} catch (QiniuException ex) {
			Response r = ex.response;
			log.error(r.toString());
			try {
				log.error(r.bodyString());
			} catch (QiniuException ex2) {
				//ignore
			}
		}
		return null;
	}

	/**
	 * @param bucket      bucket
	 * @param fileName    fileName
	 * @param inputStream inputStream
	 * @return filePath or prefix+filePath
	 */
	public String upload(String bucket, String fileName, InputStream inputStream) {
		try {
			String upToken = auth.uploadToken(bucket);
			Response response = uploadManager.put(inputStream, fileName, upToken, null, null);
			DefaultPutRet putRet = JSONObject.parseObject(response.bodyString(), DefaultPutRet.class);
			return ossProperties.getPrefix() == null ? putRet.key : ossProperties.getPrefix() + putRet.key;
		} catch (QiniuException ex) {
			Response r = ex.response;
			log.error(r.toString());
			try {
				log.error(r.bodyString());
			} catch (QiniuException ex2) {
				//ignore
			}
		}
		return null;
	}
}
